{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Labeling: Tail Sets\n",
    "\n",
    "Tail set labels are a classification labeling technique introduced in the following paper: \"[Huerta, R., Corbacho, F. and\n",
    "Elkan, C., 2013. Nonlinear support vector machines can systematically identify stocks with high and low future returns.\n",
    "Algorithmic Finance, 2(1), pp.45-58.](https://content.iospress.com/download/algorithmic-finance/af016?id=algorithmic-finance%2Faf016)\n",
    "\n",
    "A tail set is defined to be a group of assets whose volatility-adjusted price change is in the highest or lowest\n",
    "quantile, for example the highest or lowest 5%.\n",
    "\n",
    "A classification model is then fit using these labels to determine which stocks to buy and sell, for a long / short\n",
    "portfolio.\n",
    "\n",
    "We label the y variable using the tail set labeling technique, which makes up the positive and negative (1, -1) classes\n",
    "of the training data. The original paper investigates the performance of 3 types of metrics on which the tail sets are\n",
    "built:\n",
    "\n",
    "1. Real returns\n",
    "2. Residual alpha after regression on the sector index\n",
    "3. Volatility-adjusted returns\n",
    "\n",
    "For our particular implementation, we have focused on the volatility-adjusted returns."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from mlfinlab.labeling import TailSetLabels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>EEM</th>\n",
       "      <th>EWG</th>\n",
       "      <th>TIP</th>\n",
       "      <th>EWJ</th>\n",
       "      <th>EFA</th>\n",
       "      <th>IEF</th>\n",
       "      <th>EWQ</th>\n",
       "      <th>EWU</th>\n",
       "      <th>XLB</th>\n",
       "      <th>XLE</th>\n",
       "      <th>...</th>\n",
       "      <th>XLU</th>\n",
       "      <th>EPP</th>\n",
       "      <th>FXI</th>\n",
       "      <th>VGK</th>\n",
       "      <th>VPL</th>\n",
       "      <th>SPY</th>\n",
       "      <th>TLT</th>\n",
       "      <th>BND</th>\n",
       "      <th>CSJ</th>\n",
       "      <th>DIA</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2008-01-02</th>\n",
       "      <td>49.273335</td>\n",
       "      <td>35.389999</td>\n",
       "      <td>106.639999</td>\n",
       "      <td>52.919998</td>\n",
       "      <td>78.220001</td>\n",
       "      <td>87.629997</td>\n",
       "      <td>37.939999</td>\n",
       "      <td>47.759998</td>\n",
       "      <td>41.299999</td>\n",
       "      <td>79.500000</td>\n",
       "      <td>...</td>\n",
       "      <td>42.090000</td>\n",
       "      <td>51.173328</td>\n",
       "      <td>55.983330</td>\n",
       "      <td>74.529999</td>\n",
       "      <td>67.309998</td>\n",
       "      <td>144.929993</td>\n",
       "      <td>94.379997</td>\n",
       "      <td>77.360001</td>\n",
       "      <td>101.400002</td>\n",
       "      <td>130.630005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-01-03</th>\n",
       "      <td>49.716667</td>\n",
       "      <td>35.290001</td>\n",
       "      <td>107.000000</td>\n",
       "      <td>53.119999</td>\n",
       "      <td>78.349998</td>\n",
       "      <td>87.809998</td>\n",
       "      <td>37.919998</td>\n",
       "      <td>48.060001</td>\n",
       "      <td>42.049999</td>\n",
       "      <td>80.440002</td>\n",
       "      <td>...</td>\n",
       "      <td>42.029999</td>\n",
       "      <td>51.293331</td>\n",
       "      <td>55.599998</td>\n",
       "      <td>74.800003</td>\n",
       "      <td>67.500000</td>\n",
       "      <td>144.860001</td>\n",
       "      <td>94.250000</td>\n",
       "      <td>77.459999</td>\n",
       "      <td>101.519997</td>\n",
       "      <td>130.740005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-01-04</th>\n",
       "      <td>48.223331</td>\n",
       "      <td>34.599998</td>\n",
       "      <td>106.970001</td>\n",
       "      <td>51.759998</td>\n",
       "      <td>76.570000</td>\n",
       "      <td>88.040001</td>\n",
       "      <td>36.990002</td>\n",
       "      <td>46.919998</td>\n",
       "      <td>40.779999</td>\n",
       "      <td>77.500000</td>\n",
       "      <td>...</td>\n",
       "      <td>42.349998</td>\n",
       "      <td>49.849998</td>\n",
       "      <td>54.536671</td>\n",
       "      <td>72.980003</td>\n",
       "      <td>65.769997</td>\n",
       "      <td>141.309998</td>\n",
       "      <td>94.269997</td>\n",
       "      <td>77.550003</td>\n",
       "      <td>101.650002</td>\n",
       "      <td>128.169998</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-01-07</th>\n",
       "      <td>48.576668</td>\n",
       "      <td>34.630001</td>\n",
       "      <td>106.949997</td>\n",
       "      <td>51.439999</td>\n",
       "      <td>76.650002</td>\n",
       "      <td>88.199997</td>\n",
       "      <td>37.259998</td>\n",
       "      <td>47.060001</td>\n",
       "      <td>40.220001</td>\n",
       "      <td>77.199997</td>\n",
       "      <td>...</td>\n",
       "      <td>43.230000</td>\n",
       "      <td>50.416672</td>\n",
       "      <td>56.116669</td>\n",
       "      <td>72.949997</td>\n",
       "      <td>65.650002</td>\n",
       "      <td>141.190002</td>\n",
       "      <td>94.680000</td>\n",
       "      <td>77.570000</td>\n",
       "      <td>101.720001</td>\n",
       "      <td>128.059998</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-01-08</th>\n",
       "      <td>48.200001</td>\n",
       "      <td>34.389999</td>\n",
       "      <td>107.029999</td>\n",
       "      <td>51.320000</td>\n",
       "      <td>76.220001</td>\n",
       "      <td>88.389999</td>\n",
       "      <td>36.970001</td>\n",
       "      <td>46.400002</td>\n",
       "      <td>39.599998</td>\n",
       "      <td>75.849998</td>\n",
       "      <td>...</td>\n",
       "      <td>43.240002</td>\n",
       "      <td>49.566669</td>\n",
       "      <td>55.326672</td>\n",
       "      <td>72.400002</td>\n",
       "      <td>65.360001</td>\n",
       "      <td>138.910004</td>\n",
       "      <td>94.570000</td>\n",
       "      <td>77.650002</td>\n",
       "      <td>101.739998</td>\n",
       "      <td>125.849998</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 23 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                  EEM        EWG         TIP        EWJ        EFA        IEF  \\\n",
       "Date                                                                            \n",
       "2008-01-02  49.273335  35.389999  106.639999  52.919998  78.220001  87.629997   \n",
       "2008-01-03  49.716667  35.290001  107.000000  53.119999  78.349998  87.809998   \n",
       "2008-01-04  48.223331  34.599998  106.970001  51.759998  76.570000  88.040001   \n",
       "2008-01-07  48.576668  34.630001  106.949997  51.439999  76.650002  88.199997   \n",
       "2008-01-08  48.200001  34.389999  107.029999  51.320000  76.220001  88.389999   \n",
       "\n",
       "                  EWQ        EWU        XLB        XLE  ...        XLU  \\\n",
       "Date                                                    ...              \n",
       "2008-01-02  37.939999  47.759998  41.299999  79.500000  ...  42.090000   \n",
       "2008-01-03  37.919998  48.060001  42.049999  80.440002  ...  42.029999   \n",
       "2008-01-04  36.990002  46.919998  40.779999  77.500000  ...  42.349998   \n",
       "2008-01-07  37.259998  47.060001  40.220001  77.199997  ...  43.230000   \n",
       "2008-01-08  36.970001  46.400002  39.599998  75.849998  ...  43.240002   \n",
       "\n",
       "                  EPP        FXI        VGK        VPL         SPY        TLT  \\\n",
       "Date                                                                            \n",
       "2008-01-02  51.173328  55.983330  74.529999  67.309998  144.929993  94.379997   \n",
       "2008-01-03  51.293331  55.599998  74.800003  67.500000  144.860001  94.250000   \n",
       "2008-01-04  49.849998  54.536671  72.980003  65.769997  141.309998  94.269997   \n",
       "2008-01-07  50.416672  56.116669  72.949997  65.650002  141.190002  94.680000   \n",
       "2008-01-08  49.566669  55.326672  72.400002  65.360001  138.910004  94.570000   \n",
       "\n",
       "                  BND         CSJ         DIA  \n",
       "Date                                           \n",
       "2008-01-02  77.360001  101.400002  130.630005  \n",
       "2008-01-03  77.459999  101.519997  130.740005  \n",
       "2008-01-04  77.550003  101.650002  128.169998  \n",
       "2008-01-07  77.570000  101.720001  128.059998  \n",
       "2008-01-08  77.650002  101.739998  125.849998  \n",
       "\n",
       "[5 rows x 23 columns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Import price data\n",
    "data = pd.read_csv('../Sample-Data/stock_prices.csv', index_col='Date', parse_dates=True)\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create tail set labels\n",
    "labels = TailSetLabels(data, window=180, mean_abs_dev=True)\n",
    "pos_set, neg_set, matrix_set = labels.get_tail_sets()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2008-09-18    [EEM, EFA, FXI]\n",
       "2008-09-19    [EEM, EWQ, CSJ]\n",
       "2008-09-22    [TIP, LQD, EPP]\n",
       "2008-09-23    [IEF, XLK, BND]\n",
       "2008-09-24    [EWJ, EPP, CSJ]\n",
       "dtype: object"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pos_set.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2008-09-18    [TIP, BND, CSJ]\n",
       "2008-09-19    [IEF, XLK, TLT]\n",
       "2008-09-22    [EEM, XLF, CSJ]\n",
       "2008-09-23    [TIP, VGK, SPY]\n",
       "2008-09-24    [TIP, XLB, LQD]\n",
       "dtype: object"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "neg_set.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>EEM</th>\n",
       "      <th>EWG</th>\n",
       "      <th>TIP</th>\n",
       "      <th>EWJ</th>\n",
       "      <th>EFA</th>\n",
       "      <th>IEF</th>\n",
       "      <th>EWQ</th>\n",
       "      <th>EWU</th>\n",
       "      <th>XLB</th>\n",
       "      <th>XLE</th>\n",
       "      <th>...</th>\n",
       "      <th>XLU</th>\n",
       "      <th>EPP</th>\n",
       "      <th>FXI</th>\n",
       "      <th>VGK</th>\n",
       "      <th>VPL</th>\n",
       "      <th>SPY</th>\n",
       "      <th>TLT</th>\n",
       "      <th>BND</th>\n",
       "      <th>CSJ</th>\n",
       "      <th>DIA</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2008-09-18</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-09-19</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>-1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-09-22</th>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-09-23</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-09-24</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>-1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 23 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            EEM  EWG  TIP  EWJ  EFA  IEF  EWQ  EWU  XLB  XLE  ...  XLU  EPP  \\\n",
       "Date                                                          ...             \n",
       "2008-09-18    1    0   -1    0    1    0    0    0    0    0  ...    0    0   \n",
       "2008-09-19    1    0    0    0    0   -1    1    0    0    0  ...    0    0   \n",
       "2008-09-22   -1    0    1    0    0    0    0    0    0    0  ...    0    1   \n",
       "2008-09-23    0    0   -1    0    0    1    0    0    0    0  ...    0    0   \n",
       "2008-09-24    0    0   -1    1    0    0    0    0   -1    0  ...    0    1   \n",
       "\n",
       "            FXI  VGK  VPL  SPY  TLT  BND  CSJ  DIA  \n",
       "Date                                                \n",
       "2008-09-18    1    0    0    0    0   -1   -1    0  \n",
       "2008-09-19    0    0    0    0   -1    0    1    0  \n",
       "2008-09-22    0    0    0    0    0    0   -1    0  \n",
       "2008-09-23    0   -1    0   -1    0    1    0    0  \n",
       "2008-09-24    0    0    0    0    0    0    1    0  \n",
       "\n",
       "[5 rows x 23 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix_set.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>EEM</th>\n",
       "      <th>EWG</th>\n",
       "      <th>TIP</th>\n",
       "      <th>EWJ</th>\n",
       "      <th>EFA</th>\n",
       "      <th>IEF</th>\n",
       "      <th>EWQ</th>\n",
       "      <th>EWU</th>\n",
       "      <th>XLB</th>\n",
       "      <th>XLE</th>\n",
       "      <th>...</th>\n",
       "      <th>XLU</th>\n",
       "      <th>EPP</th>\n",
       "      <th>FXI</th>\n",
       "      <th>VGK</th>\n",
       "      <th>VPL</th>\n",
       "      <th>SPY</th>\n",
       "      <th>TLT</th>\n",
       "      <th>BND</th>\n",
       "      <th>CSJ</th>\n",
       "      <th>DIA</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2008-09-18</th>\n",
       "      <td>4.508371</td>\n",
       "      <td>3.155230</td>\n",
       "      <td>-3.111303</td>\n",
       "      <td>3.587530</td>\n",
       "      <td>3.848416</td>\n",
       "      <td>-2.785101</td>\n",
       "      <td>2.938655</td>\n",
       "      <td>3.345493</td>\n",
       "      <td>-0.020650</td>\n",
       "      <td>1.453407</td>\n",
       "      <td>...</td>\n",
       "      <td>3.251648</td>\n",
       "      <td>3.474827</td>\n",
       "      <td>5.034605</td>\n",
       "      <td>3.431751</td>\n",
       "      <td>3.472486</td>\n",
       "      <td>2.568232</td>\n",
       "      <td>-2.335827</td>\n",
       "      <td>-4.461541</td>\n",
       "      <td>-5.097691</td>\n",
       "      <td>2.696179</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-09-19</th>\n",
       "      <td>6.105422</td>\n",
       "      <td>5.611920</td>\n",
       "      <td>1.188319</td>\n",
       "      <td>4.135636</td>\n",
       "      <td>5.575620</td>\n",
       "      <td>-4.072046</td>\n",
       "      <td>5.794786</td>\n",
       "      <td>4.375463</td>\n",
       "      <td>4.014838</td>\n",
       "      <td>3.036212</td>\n",
       "      <td>...</td>\n",
       "      <td>2.033955</td>\n",
       "      <td>3.187363</td>\n",
       "      <td>4.074290</td>\n",
       "      <td>5.525651</td>\n",
       "      <td>4.380080</td>\n",
       "      <td>2.844334</td>\n",
       "      <td>-4.859581</td>\n",
       "      <td>3.678729</td>\n",
       "      <td>21.788846</td>\n",
       "      <td>2.861168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-09-22</th>\n",
       "      <td>-3.401323</td>\n",
       "      <td>-2.110389</td>\n",
       "      <td>0.805733</td>\n",
       "      <td>-2.696409</td>\n",
       "      <td>-2.764349</td>\n",
       "      <td>-0.485610</td>\n",
       "      <td>-2.454268</td>\n",
       "      <td>-1.944645</td>\n",
       "      <td>-0.926679</td>\n",
       "      <td>-0.852665</td>\n",
       "      <td>...</td>\n",
       "      <td>-2.207600</td>\n",
       "      <td>0.248532</td>\n",
       "      <td>-2.401272</td>\n",
       "      <td>-1.306426</td>\n",
       "      <td>-1.494577</td>\n",
       "      <td>-1.939615</td>\n",
       "      <td>-0.016243</td>\n",
       "      <td>-0.789467</td>\n",
       "      <td>-13.152190</td>\n",
       "      <td>-2.390453</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-09-23</th>\n",
       "      <td>-1.583086</td>\n",
       "      <td>-1.319794</td>\n",
       "      <td>-2.360897</td>\n",
       "      <td>-0.709549</td>\n",
       "      <td>-1.125958</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-1.373446</td>\n",
       "      <td>-1.278954</td>\n",
       "      <td>-1.679985</td>\n",
       "      <td>-1.603999</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.519144</td>\n",
       "      <td>-0.750959</td>\n",
       "      <td>-0.896277</td>\n",
       "      <td>-2.227828</td>\n",
       "      <td>-0.797525</td>\n",
       "      <td>-1.926089</td>\n",
       "      <td>-0.588874</td>\n",
       "      <td>0.947756</td>\n",
       "      <td>-0.059872</td>\n",
       "      <td>-1.581367</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-09-24</th>\n",
       "      <td>0.574284</td>\n",
       "      <td>-0.168215</td>\n",
       "      <td>-1.131977</td>\n",
       "      <td>1.061057</td>\n",
       "      <td>0.231426</td>\n",
       "      <td>0.598360</td>\n",
       "      <td>-0.357022</td>\n",
       "      <td>-0.084261</td>\n",
       "      <td>-1.416672</td>\n",
       "      <td>0.142575</td>\n",
       "      <td>...</td>\n",
       "      <td>0.803514</td>\n",
       "      <td>0.901233</td>\n",
       "      <td>0.469463</td>\n",
       "      <td>-0.227092</td>\n",
       "      <td>0.575725</td>\n",
       "      <td>0.270343</td>\n",
       "      <td>0.673304</td>\n",
       "      <td>-0.039926</td>\n",
       "      <td>0.956826</td>\n",
       "      <td>-0.161457</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 23 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                 EEM       EWG       TIP       EWJ       EFA       IEF  \\\n",
       "Date                                                                     \n",
       "2008-09-18  4.508371  3.155230 -3.111303  3.587530  3.848416 -2.785101   \n",
       "2008-09-19  6.105422  5.611920  1.188319  4.135636  5.575620 -4.072046   \n",
       "2008-09-22 -3.401323 -2.110389  0.805733 -2.696409 -2.764349 -0.485610   \n",
       "2008-09-23 -1.583086 -1.319794 -2.360897 -0.709549 -1.125958  0.000000   \n",
       "2008-09-24  0.574284 -0.168215 -1.131977  1.061057  0.231426  0.598360   \n",
       "\n",
       "                 EWQ       EWU       XLB       XLE  ...       XLU       EPP  \\\n",
       "Date                                                ...                       \n",
       "2008-09-18  2.938655  3.345493 -0.020650  1.453407  ...  3.251648  3.474827   \n",
       "2008-09-19  5.794786  4.375463  4.014838  3.036212  ...  2.033955  3.187363   \n",
       "2008-09-22 -2.454268 -1.944645 -0.926679 -0.852665  ... -2.207600  0.248532   \n",
       "2008-09-23 -1.373446 -1.278954 -1.679985 -1.603999  ... -1.519144 -0.750959   \n",
       "2008-09-24 -0.357022 -0.084261 -1.416672  0.142575  ...  0.803514  0.901233   \n",
       "\n",
       "                 FXI       VGK       VPL       SPY       TLT       BND  \\\n",
       "Date                                                                     \n",
       "2008-09-18  5.034605  3.431751  3.472486  2.568232 -2.335827 -4.461541   \n",
       "2008-09-19  4.074290  5.525651  4.380080  2.844334 -4.859581  3.678729   \n",
       "2008-09-22 -2.401272 -1.306426 -1.494577 -1.939615 -0.016243 -0.789467   \n",
       "2008-09-23 -0.896277 -2.227828 -0.797525 -1.926089 -0.588874  0.947756   \n",
       "2008-09-24  0.469463 -0.227092  0.575725  0.270343  0.673304 -0.039926   \n",
       "\n",
       "                  CSJ       DIA  \n",
       "Date                             \n",
       "2008-09-18  -5.097691  2.696179  \n",
       "2008-09-19  21.788846  2.861168  \n",
       "2008-09-22 -13.152190 -2.390453  \n",
       "2008-09-23  -0.059872 -1.581367  \n",
       "2008-09-24   0.956826 -0.161457  \n",
       "\n",
       "[5 rows x 23 columns]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "labels.vol_adj_rets.dropna().head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## How to use these labels in practice?\n",
    "\n",
    "The tail set labels from the code above returns the names of the assets which should be labeled with a positive or\n",
    "negative label. Its important to note that the model you  would develop is a many to one model, in that it has many\n",
    "x variables and only one y variable. The model is a binary classifier.\n",
    "\n",
    "The model is trained on the training data and then used to score every security in the test data (on a given day).\n",
    "Example: On December 1st 2019, the strategy needs to rebalance its positions, we score all 100 securities in our tradable\n",
    "universe and then rank the outputs in a top down fashion. We form a long / short portfolio by going long the top 10\n",
    "stocks and short the bottom 10 (equally weighted). We then hold the position to the next rebalance date."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
